//
//  Pantalla4.m
//  Grues
//
//  Created by Miquel Viladomat on 14/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Pantalla4.h"
#import "ZonaBlavaAppDelegate.h"
@implementation Pantalla4
@synthesize codiagent;
@synthesize codidenuncia;
@synthesize ladata;
@synthesize hora;
@synthesize matricula;
@synthesize color;
@synthesize marca;
@synthesize model;
@synthesize adreca;
@synthesize foto1;
@synthesize foto2;
@synthesize foto3;
@synthesize foto4;
-(void) connection:(NSURLConnection *) connection 
didReceiveResponse:(NSURLResponse *) response {
    [webData setLength: 0];
     
}
-(void) connection:(NSURLConnection *) connection 
     didReceiveData:(NSData *) data {
    [webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection 
  didFailWithError:(NSError *) error {
    NSLog(error.localizedDescription);
    [webData release];
    [connection release];
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
    
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
    NSString *theXML = [[NSString alloc] 
                        initWithBytes: [webData mutableBytes] 
                        length:[webData length] 
                        encoding:NSUTF8StringEncoding];
    if (xmlParser)
    {
        [xmlParser release];
    }    
    xmlParser = [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];
    
    [theXML release];     
    
    [connection release];
    [webData release];
}
-(void) parser:(NSXMLParser *) parser 
didStartElement:(NSString *) elementName 
  namespaceURI:(NSString *) namespaceURI 
 qualifiedName:(NSString *) qName
    attributes:(NSDictionary *) attributeDict {
    
    if( [elementName isEqualToString:@"Codi"])
    {
        if (!soapResults)
        {
            soapResults = [[NSMutableString alloc] init];
        }
        elementFound = YES;
    }
    if( [elementName isEqualToString:@"Resposta"])
    {
        
            resposta = [[NSMutableString alloc] init];
            elementFound2 = YES;
    }
    if( [elementName isEqualToString:@"temps"])
    {
        
        resposta2 = [[NSMutableString alloc] init];
        elementFound3 = YES;
    }
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
    if (elementFound)
    {
        [soapResults appendString: string];
                
    }
    if (elementFound2)
    {
        [resposta appendString: string];
    }
    if (elementFound3)
    {
        [resposta2 appendString: string];
    }
}
-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName
{
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];

    if ([elementName isEqualToString:@"Codi"])
    {
        dataCenter.codidenunciav=soapResults;
        [codidenuncia setText:soapResults];
        [codidenuncia setTextColor:[UIColor whiteColor]];
        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType =[NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];
        NSData *imageData = UIImageJPEGRepresentation(dataCenter.foto1v, 0.7);
        NSString *urlString =[NSString stringWithFormat: @"http://www.giroassist.com/picimg/index1.php?codi=%@",soapResults];    
        NSMutableURLRequest *request=[[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];
        
        [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
        
        NSMutableData *body =  [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n@"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];
        
        NSData *imageData2 = UIImageJPEGRepresentation(dataCenter.foto2v, 0.7);
        NSString *urlString2 =[NSString stringWithFormat: @"http://www.giroassist.com/picimg/index2.php?codi=%@",soapResults];    
        NSMutableURLRequest *request2=[[[NSMutableURLRequest alloc] init] autorelease];
        [request2 setURL:[NSURL URLWithString:urlString2]];
        [request2 setHTTPMethod:@"POST"]; 
        
        [request2 addValue:contentType forHTTPHeaderField:@"Content-Type"];
        
        NSMutableData *body2 =  [NSMutableData data];
        [body2 appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body2 appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n@"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body2 appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body2 appendData:[NSData dataWithData:imageData2]];
        [body2 appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request2 setHTTPBody:body2];
        [NSURLConnection sendSynchronousRequest:request2 returningResponse:nil error:nil];
        NSData *imageData3 = UIImageJPEGRepresentation(dataCenter.foto3v, 0.7);
        NSString *urlString3 =[NSString stringWithFormat:@"http://www.giroassist.com/picimg/index3.php?codi=%@",soapResults];
        
        NSMutableURLRequest *request3=[[[NSMutableURLRequest alloc] init] autorelease];
        [request3 setURL:[NSURL URLWithString:urlString3]];
        [request3 setHTTPMethod:@"POST"];
        
        [request3 addValue:contentType forHTTPHeaderField:@"Content-Type"];
        
        NSMutableData *body3 =  [NSMutableData data];
        [body3 appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body3 appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n@"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body3 appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body3 appendData:[NSData dataWithData:imageData3]];
        [body3 appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request3 setHTTPBody:body3];
        [NSURLConnection sendSynchronousRequest:request3 returningResponse:nil error:nil];
        NSData *imageData4 = UIImageJPEGRepresentation(dataCenter.foto4v, 0.7);
        NSString *urlString4 =[NSString stringWithFormat:@"http://www.giroassist.com/picimg/index4.php?codi=%@",soapResults];
        
        NSMutableURLRequest *request4=[[[NSMutableURLRequest alloc] init] autorelease];
        [request4 setURL:[NSURL URLWithString:urlString4]];
        [request4 setHTTPMethod:@"POST"];
        
        [request4 addValue:contentType forHTTPHeaderField:@"Content-Type"];
        ;NSMutableData *body4 =[NSMutableData data];
        [body4 appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body4 appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n@"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body4 appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body4 appendData:[NSData dataWithData:imageData4]];
        [body4 appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request4 setHTTPBody:body4];
        [NSURLConnection sendSynchronousRequest:request4 returningResponse:nil error:nil];

    }
}


-(IBAction)pushUpload{
    [msg1 setHidden:TRUE];
    [msg2 setHidden:TRUE];
    [botoenvia setHidden:TRUE];
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *temp=[dataCenter.posicio2v stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *urlString =[NSString stringWithFormat:@"http://www.giroassist.com/picimg/index0.php?usuari=1&marca=%@&model=%@&color=%@&matricula=%@&situacio=%@&lat=%@&long=%@&sancio=%@&ladata=%@&hora=%@",dataCenter.marcav,dataCenter.modelv,dataCenter.colorv,dataCenter.matriculav,temp,dataCenter.latv,dataCenter.longv,dataCenter.codimotiuv,dataCenter.datav,dataCenter.horav];
    
    NSMutableURLRequest *request=[[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];    
    
    
    conn=[[NSURLConnection alloc]initWithRequest:request delegate:self];
    if (conn) {
        webData = [[NSMutableData data] retain];
    }    
   
}
-(IBAction)novaDenuncia{
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
    dataCenter.marcav=@"";
    dataCenter.modelv=@"";
    dataCenter.colorv=@"";
    dataCenter.matriculav=@"";
    dataCenter.foto1v=nil;
    dataCenter.foto2v=nil;
    dataCenter.foto3v=nil;
    dataCenter.foto4v=nil;
    dataCenter.estat=@"entrant";
    codidenuncia.text=@"PENDENT";
    [codidenuncia setTextColor:[UIColor grayColor]];
    ladata.text=@"";
    hora.text=@"";
    matricula.text=@"";
    color.text=@"";
    marca.text=@"";
    model.text=@"";
    adreca.text=@"";
    [msg1 setHidden:FALSE];
    [msg2 setHidden:FALSE];
    [botoenvia setHidden:FALSE];
    UIImage* imatgecamera = [UIImage imageNamed:@"iconacamera.png"];
    foto1.image=imatgecamera;
    foto2.image=imatgecamera;
    foto3.image=imatgecamera;
    foto4.image=imatgecamera;
    NSDate *now = [NSDate date];
    NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];
    NSArray *arr = [strDate componentsSeparatedByString:@" "];
    NSString *str;
    str = [arr objectAtIndex:0];
    
    NSArray *arr_my = [str componentsSeparatedByString:@"-"];
    
    dataCenter.datav=[NSString stringWithFormat:@"%@/%@/%@",[arr_my objectAtIndex:2],[arr_my objectAtIndex:1],[arr_my objectAtIndex:0]];
    str = [arr objectAtIndex:1];
    NSArray *arr_my2 = [str componentsSeparatedByString:@":"];
    dataCenter.horav=[NSString stringWithFormat:@"%@:%@",[arr_my2 objectAtIndex:0],[arr_my2 objectAtIndex:1]];
    
}
-(void)viewWillAppear:(BOOL)animated{
    ZonaBlavaAppDelegate *dataCenter=(ZonaBlavaAppDelegate *)[[UIApplication sharedApplication] delegate];
    marca.text=dataCenter.marcav;
    model.text=dataCenter.modelv;
    color.text=dataCenter.colorv;
    matricula.text=dataCenter.matriculav;
    adreca.text=dataCenter.posicio2v;
    codiagent.text=dataCenter.codiagentv;
    ladata.text=dataCenter.datav;
    hora.text=dataCenter.horav;
    UIImage* imatgecamera = [UIImage imageNamed:@"iconacamera.png"];
    if(dataCenter.foto1v==nil){
        foto1.image=imatgecamera;
    }
    else{
        foto1.image=dataCenter.foto1v;
    }
    if(dataCenter.foto2v==nil){
        foto2.image=imatgecamera;
    }
    else{
        foto2.image=dataCenter.foto2v;
    }
    if(dataCenter.foto3v==nil){
        foto3.image=imatgecamera;
    }
    else{
        foto3.image=dataCenter.foto3v;
    }
    if(dataCenter.foto4v==nil){
        foto4.image=imatgecamera;
    }
    else{
        foto4.image=dataCenter.foto4v;
    }
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
